do "E:\yungyu\lottery_baby\do\set_environment.do"

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear

keep if norm == -1
keep houseid current pers_idn pers_gender age pers_earnings pers_income
rename pers_gender gender
rename pers_earnings earnings
rename pers_income income

gen lifetime_earnings_at60 = earnings
gen lifetime_income_at60 = income

gen age_pre1 = age
gen earnings_pre1 = earnings
gen income_pre1 = income

forv i = 1(1)41{
	merge m:1 age gender using "$wdata/earnings_growthrate.dta", update replace
	drop if _m == 2
	drop _m

	replace age = age + 1
	
	replace earnings = earnings * (1+median_earnings_gr)
	replace income = income * (1+median_income_gr)
	
	replace lifetime_earnings_at60 = lifetime_earnings_at60 + earnings / (1.02)^`i' if age <= 60
	replace lifetime_income_at60 = lifetime_income_at60 + income / (1.02)^`i' if age <= 60
}

save "$wdata/lifetime_income.dta", replace
